package com.xiaoze.databaseTableControl.controller;

import java.util.List;

import com.xiaoze.databaseTableControl.domain.DatabseTableSql;
import com.xiaoze.databaseTableControl.service.IDatabseTableSqlService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xiaoze.common.annotation.Log;
import com.xiaoze.common.core.controller.BaseController;
import com.xiaoze.common.core.domain.AjaxResult;
import com.xiaoze.common.enums.BusinessType;
import com.xiaoze.common.utils.poi.ExcelUtil;
import com.xiaoze.common.core.page.TableDataInfo;

/**
 * 用户提交的sql记录Controller
 *
 * @author hhz
 * @date 2024-12-16
 */
@RestController
@RequestMapping("/databaseTableSql/sql")
public class DatabseTableSqlController extends BaseController {
    @Autowired
    private IDatabseTableSqlService databseTableSqlService;

    /**
     * 查询用户提交的sql记录列表
     */
    @PreAuthorize("@ss.hasPermi('question:sql:list')")
    @GetMapping("/list")
    public TableDataInfo list(DatabseTableSql databseTableSql) {
        startPage();
        List<DatabseTableSql> list = databseTableSqlService.selectDatabseTableSqlList(databseTableSql);
        return getDataTable(list);
    }

    /**
     * 导出用户提交的sql记录列表
     */
    @PreAuthorize("@ss.hasPermi('question:sql:export')")
    @Log(title = "用户提交的sql记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DatabseTableSql databseTableSql) {
        List<DatabseTableSql> list = databseTableSqlService.selectDatabseTableSqlList(databseTableSql);
        ExcelUtil<DatabseTableSql> util = new ExcelUtil<DatabseTableSql>(DatabseTableSql.class);
        return util.exportExcel(list, "sql");
    }

    /**
     * 获取用户提交的sql记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('question:sql:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(databseTableSqlService.selectDatabseTableSqlById(id));
    }

    /**
     * 新增用户提交的sql记录
     */
    @PreAuthorize("@ss.hasPermi('question:sql:add')")
    @Log(title = "用户提交的sql记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DatabseTableSql databseTableSql) {
        return toAjax(databseTableSqlService.insertDatabseTableSql(databseTableSql));
    }

    /**
     * 修改用户提交的sql记录
     */
    @PreAuthorize("@ss.hasPermi('question:sql:edit')")
    @Log(title = "用户提交的sql记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DatabseTableSql databseTableSql) {
        return toAjax(databseTableSqlService.updateDatabseTableSql(databseTableSql));
    }

    /**
     * 删除用户提交的sql记录
     */
    @PreAuthorize("@ss.hasPermi('question:sql:remove')")
    @Log(title = "用户提交的sql记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(databseTableSqlService.deleteDatabseTableSqlByIds(ids));
    }
}
